/*
 * Copyright (C), 2017, 上海云首信息科技有限公司
 *
 * Author: 邓哲航
 * Date: 2018/5/13 下午8:13
 * History:
 * <author>    <time>    <version>    <desc>
 *
 */
package com.leaptocloud.easycloud.demo.resource;

import com.google.gson.Gson;
import com.leaptocloud.easycloud.demo.dao.UserDao;
import com.leaptocloud.easycloud.demo.domain.User;
import com.leaptocloud.easycloud.demo.service.UserService;
import com.leaptocloud.easycloud.demo.shiro.XAuthToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 邓哲航
 * @see
 * @since 2.3.8
 */
@Path("/demo")
@Component
@RequiresAuthentication
public class TestResource {

    private static Logger logger = LoggerFactory.getLogger(TestResource.class);

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserService userService;

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public Response getUser() {
        return Response.ok("DEMO").build();
    }

    @GET
    @Path("/uriInfo")
    @Produces({MediaType.APPLICATION_JSON})
    @Consumes({MediaType.TEXT_XML})
    public Response context(@Context UriInfo uriInfo) {
        logger.info("get uriInfo");
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isAuthenticated()){
            XAuthToken token = new XAuthToken("password");
            subject.login(token);
        }
        System.out.println(subject.isAuthenticated());
        System.out.println(subject.getPrincipal());

        return Response.ok(uriInfo.getAbsolutePath()).build();
    }

    @GET
    @Path("/users/{id}")
    @Produces({MediaType.APPLICATION_JSON})
    @Consumes({MediaType.TEXT_XML})
    public Response getUser(@PathParam("id") Long id) throws UnsupportedEncodingException {
        System.out.println("get user " + id);

        User u = userDao.findUser(id);
        Gson gson = new Gson();
        return Response.ok(u).build();
//        return Response.status(Response.Status.BAD_REQUEST).header("errMsg", URLEncoder.encode("参数错误", "UTF-8")).build();
    }

    @GET
    @Path("/users")
    @Produces({MediaType.APPLICATION_JSON})
    @Consumes({MediaType.TEXT_XML})
//    @RequiresRoles("admin")
    public Response listUsers() {
        System.out.println("list users");

        List<User> userList = new LinkedList<>();
        User u = userDao.findUser(1L);
        User u2 = userDao.findUser(1L);
        User.Hobbit hobbit = new User.Hobbit();
        hobbit.setName("ping pong");
        hobbit.setPeriod(10);
        List<User.Hobbit> list = new LinkedList<>();
        list.add(hobbit);

        u.setHobbits(list);
        userList.add(u);
        u2.setHobbits(list);
        userList.add(u2);
        return Response.ok(userList).build();
    }

    @POST
    @Path("/users")
    @Produces({MediaType.APPLICATION_JSON})
    @Consumes({MediaType.APPLICATION_JSON})
    public Response addUser() throws URISyntaxException {
        User u = new User();
        u.setLogin("dzh");
        u.setId(1L);

        userService.addUsers("dd");
        return Response.created(new URI("/users/1")).build();
    }
}
